package com.snap.vseries.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.analyze.AlgoParams;
import com.snap.vseries.base.BaseFragment;
import com.snap.vseries.bean.ExpInfo;
import com.snap.vseries.dialog.NumberInputDialog;

import java.math.BigDecimal;

import butterknife.BindView;

@SuppressLint("ValidFragment")
public class InnerParamFragment extends BaseFragment {

    @BindView(R.id.algo_rg)
    RadioGroup algoRg;

    @BindView(R.id.bl_rg)
    RadioGroup blRg;
    @BindView(R.id.def_bl_tv)
    TextView defBlTv;
    @BindView(R.id.max_slope_tv)
    TextView maxSlopeTv;

    @BindView(R.id.bl_opt_sw)
    SwitchCompat blOptSw;
    @BindView(R.id.bl_opt_tv)
    TextView blOptThTv;

    @BindView(R.id.def_th_tv)
    TextView defThTv;
    @BindView(R.id.min_th_tv)
    TextView minThTv;

    private ExpInfo experimentInfo;
    private AlgoParams algoParams;

    public InnerParamFragment(ExpInfo experimentInfo) {
        this.experimentInfo = experimentInfo;
        this.algoParams = experimentInfo.getAlgoParams();
    }

    @Override
    protected int layoutResId() {
        return R.layout.dialog_inner_param;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        if (algoParams.getMainAlgo().equals("origin")) {
            algoRg.check(R.id.algo_origin);
        } else {
            algoRg.check(R.id.algo_def);
        }

        if (algoParams.getBaselineMethod().equals("ab")) {
            blRg.check(R.id.bl_ab);
        } else {
            blRg.check(R.id.bl_ma);
        }
        defBlTv.setText(String.valueOf(algoParams.getDefBaselineStart()));
        defBlTv.setOnClickListener(v ->
                new NumberInputDialog()
                        .setParam(0, 1, Integer.parseInt(defBlTv.getText().toString()), 15, "默认基线起点", "",
                                value -> defBlTv.setText(String.valueOf((int) value)))
                        .setMaxLength(2)
                        .show(getChildFragmentManager(), ""));
        maxSlopeTv.setText(String.valueOf(algoParams.getMaxSlope()));
        maxSlopeTv.setOnClickListener(v ->
                new NumberInputDialog()
                        .setParam(3, 0.001f, Float.parseFloat(maxSlopeTv.getText().toString()), 0.1f, "允许最大斜率", "",
                                value -> {
                                    BigDecimal b = new BigDecimal(String.valueOf(value));
                                    maxSlopeTv.setText(String.valueOf(b.doubleValue()));
                                })
                        .setMaxLength(5)
                        .show(getChildFragmentManager(), ""));

        blOptSw.setChecked(algoParams.isRelativeBLOpt());
        blOptThTv.setText(String.valueOf(algoParams.getRelativeBLOptThreshold()));

        defThTv.setText(String.valueOf(algoParams.getDefThreshold()));
        minThTv.setText(String.valueOf(algoParams.getMinThreshold()));
        minThTv.setOnClickListener(v ->
                new NumberInputDialog()
                        .setParam(3, 1.01f, Float.parseFloat(minThTv.getText().toString()), 2f, "最小阈值", "",
                                value -> {
                                    BigDecimal b = new BigDecimal(String.valueOf(value));
                                    minThTv.setText(String.valueOf(b.doubleValue()));
                                })
                        .setMaxLength(5)
                        .show(getChildFragmentManager(), ""));
    }

    public void apply() {
        if (algoRg.getCheckedRadioButtonId() == R.id.algo_origin) {
            algoParams.setMainAlgo("origin");
        } else {
            algoParams.setMainAlgo("default");
        }

        if (blRg.getCheckedRadioButtonId() == R.id.bl_ab) {
            algoParams.setBaselineMethod("ab");
        } else {
            algoParams.setBaselineMethod("ma");
        }
        try {
            algoParams.setDefBaselineStart(Integer.parseInt(defBlTv.getText().toString()));
            algoParams.setMaxSlope(Double.parseDouble(maxSlopeTv.getText().toString()));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        try {
            algoParams.setRelativeBLOpt(blOptSw.isChecked());
            algoParams.setRelativeBLOptThreshold(Double.parseDouble(blOptThTv.getText().toString()));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        try {
            algoParams.setMinThreshold(Double.parseDouble(minThTv.getText().toString()));
            algoParams.setDefThreshold(Double.parseDouble(defThTv.getText().toString()));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
}